package com.situ.springboot.controller;

import com.situ.springboot.pojo.Student;
import com.situ.springboot.service.IStudentService;
import com.situ.springboot.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController=@Controller+@ResponseBody
@RestController
//@CrossOrigin
public class StudentRestController {
    @Autowired
    private IStudentService studentService;

    // /students   GET
    //@RequestMapping(value = "/students", method = RequestMethod.GET)
    @GetMapping("/students")
    public JSONResult selectAll() {
        List<Student> list = studentService.selectAll();
        return JSONResult.ok(list);
    }

    // /students/1   GET
    @GetMapping("/students/{id}")
    public JSONResult selectById(@PathVariable("id") Integer id) {
        Student student = studentService.selectById(id);
        return JSONResult.ok(student);
    }

    @PostMapping("/students")
    public JSONResult add(@RequestBody  Student student) {
        System.out.println("StudentRestController.add");
        System.out.println(student);
        return JSONResult.ok("添加成功");
    }

    @PutMapping("/students")
    public JSONResult update(@RequestBody Student student) {
        System.out.println("StudentRestController.update");
        System.out.println(student);
        return JSONResult.ok("修改成功");
    }

    @DeleteMapping("/students/{id}")
    public JSONResult deleteById(@PathVariable("id") Integer id) {
        System.out.println("StudentRestController.deleteById");
        System.out.println("id: " + id);
        return JSONResult.ok("删除成功");
    }

}
